package hzst.android.form.view;

import hzst.android.R;
import hzst.android.form.ViewCreator;
import hzst.android.form.info.BaseViewInfo;
import hzst.android.form.info.FormViewInfo;
import hzst.android.form.info.FormViewInfo.FormSubmitValue;

import java.util.List;

import android.content.Context;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
 * 表格控件。由若干个单元格组成，每个单元格由标题(名称)和若干项(名称对应的一些信息，可填写或者只读)。
 * @author wt
 *
 */
public class FMFormView extends FMBaseView{
	
	private FormViewInfo info;
	private List<List<FormSubmitValue>> formValues;
	
	private int CELL_HEIGHT = 48;
	private static final int MARGIN_TBLR = 1;
	private static final int MARGIN_R = 2;
	private static final int MARGIN_B = 3;	
	private static final int MARGIN_RB = 4;
	private static final int MARGIN_NULL = 5;
	
	public FMFormView(Context context) {
		super(context);

		CELL_HEIGHT = (int) (CELL_HEIGHT*density);
		initView();
	}

	@Override
	public void setInfo(BaseViewInfo info, ViewCreator viewCreator) {
		super.setInfo(info, viewCreator);
		this.info = (FormViewInfo)info;
		formValues = this.info.getFormValues();
	}
	@Override
	protected void initView() {}
	
	@Override
	public void showView() {
		int index = 0;
		LinearLayout view = createLinearLayout(LinearLayout.VERTICAL, getLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, -1, MARGIN_NULL));
		for (int i = 0; i < formValues.size(); i++) {
			LinearLayout llay = createLinearLayout(LinearLayout.HORIZONTAL, getLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, -1, MARGIN_TBLR));
			createTitle(llay, formValues.get(i).get(0).getBroadName());
			LinearLayout llayMiddle = createLinearLayout(LinearLayout.VERTICAL, getLayoutParams(0, LayoutParams.WRAP_CONTENT, ViewCreator.WEIGHT_CONTENT, MARGIN_NULL));
			for (int j = 0; j < formValues.get(i).size(); j++) {
				FormSubmitValue sv = formValues.get(i).get(j);
				index ++;
				if(j == formValues.get(i).size()-1){
					addInnerCell(llayMiddle, sv, true,index);
				}else{
					addInnerCell(llayMiddle, sv, false,index);
				}
			}
			llay.addView(llayMiddle);
			view.addView(llay);
		}
		addView(view);
	}
	/**
	 * 
	 * @param llay
	 * @param title
	 */
	private void createTitle(LinearLayout llay,String title){
		View tvTopTitle = createCell(title, getLayoutParams(0, LayoutParams.MATCH_PARENT, ViewCreator.WEIGHT_TITLE, MARGIN_R),ViewCreator.COLOR_NAME,0,BaseViewInfo.TYPE_TEXT);
		tvTopTitle.setEnabled(true);
//		TextPaint tp = tvTopTitle.getPaint();
//		tp.setFakeBoldText(true);
		llay.addView(tvTopTitle);
	}
	
	private void addInnerCell(LinearLayout llayMiddle,FormSubmitValue submitValue,boolean isLast,int index){
		
		LinearLayout llayInner = createLinearLayout(LinearLayout.HORIZONTAL, getLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, -1, MARGIN_NULL));
		View innerTitle;
		if(BaseViewInfo.TYPE_TEXT.equals(submitValue.getValueType())){
			if(isLast){
				innerTitle = createCell(submitValue.getItemName(), getLayoutParams(0, LayoutParams.MATCH_PARENT,1, MARGIN_R),ViewCreator.COLOR_CONTENT,0,BaseViewInfo.TYPE_TEXT);
			}else{
				innerTitle = createCell(submitValue.getItemName(), getLayoutParams(0, LayoutParams.MATCH_PARENT,1, MARGIN_RB),ViewCreator.COLOR_CONTENT,0,BaseViewInfo.TYPE_TEXT);
			}
			llayInner.addView(innerTitle);
		}
		
		View tvContent;
		if(isLast){
			tvContent = createCell(submitValue.getValue(), getLayoutParams(0, CELL_HEIGHT, 1, MARGIN_NULL),ViewCreator.COLOR_CONTENT,index,submitValue.getValueType());
		}else{
			tvContent = createCell(submitValue.getValue(), getLayoutParams(0, CELL_HEIGHT, 1, MARGIN_B),ViewCreator.COLOR_CONTENT,index,submitValue.getValueType());
		}
		llayInner.setBackgroundResource(ViewCreator.COLOR_CONTENT);
		llayInner.addView(tvContent);
		
		llayMiddle.addView(llayInner);
	}
	
	private LinearLayout.LayoutParams getLayoutParams(int width,int height,int weight,int margin){
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
		if(weight >= 0){
			params.weight = weight;
		}
		int divider = (int) (0.5*density);
		switch (margin) {
		case MARGIN_TBLR:
			params.setMargins(divider, divider, divider, divider);
			break;
		case MARGIN_R:
			params.setMargins(0, 0, divider, 0);
			break;
		case MARGIN_B:
			params.setMargins(0, 0, 0, divider);
			break;
		case MARGIN_RB:
			params.setMargins(0, 0, divider, divider);
			break;
		}
		return params;
	}
	
	private LinearLayout createLinearLayout(int orientation,LinearLayout.LayoutParams params){
		LinearLayout llay = new LinearLayout(context);
		llay.setOrientation(orientation);
		llay.setLayoutParams(params);
		llay.setBackgroundColor(getResources().getColor(R.color.divider_general));
		return llay;
	}
	/**
	 * 
	 * @param text
	 * @param params
	 * @param backRes
	 * @param index
	 * @param type
	 * @return
	 */
	private View createCell(String text,LinearLayout.LayoutParams params,int backRes,final int index,String type){
		View cell = null;
		switch (type) {
		case BaseViewInfo.TYPE_TEXT:
			cell = new TextView(context);
			cell.setBackgroundResource(backRes);
			((TextView) cell).setGravity(Gravity.CENTER);
			
			((TextView) cell).setText(text);
//			tv.setTextSize(16);
//			tv.setTextColor(Color.GRAY);
			cell.setLayoutParams(params);
			cell.setPadding((int)(5*density), (int)(5*density), (int)(5*density), (int)(5*density));
//			tv.setWidth((int) (60*density*weight));
//			tv.setMinHeight((int) (40*weight*density));
//			tv.setBackgroundColor(Color.WHITE);
//			tv.setEllipsize(TextUtils.TruncateAt.MARQUEE);
			((TextView) cell).setMovementMethod(ScrollingMovementMethod.getInstance());
			if(info.isReadOnly()){
				cell.setEnabled(false);
			}
			break;
		case BaseViewInfo.TYPE_ST:
		case BaseViewInfo.TYPE_SV:

			break;
		case "Check":
			cell = new CheckBox(context);
			((CheckBox) cell).setText(text);
			break;
		}
		
		
		/*
		 * 需要编辑时启用
		 */
//		tv = new EditText(context);
//		tv.setLayoutParams(getLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, -1, MARGIN_NULL));
//		tv.setGravity(Gravity.LEFT|Gravity.BOTTOM);
//		tv.addTextChangedListener(new TextWatcher() {
//			
//			@Override
//			public void onTextChanged(CharSequence s, int start, int before, int count) {}
//			
//			@Override
//			public void beforeTextChanged(CharSequence s, int start, int count,
//					int after) {}
//			
//			@Override
//			public void afterTextChanged(Editable s) {
//				viewCreator.setSubmitValue(s.toString(), info, index);
////				viewCreator.updateViewInfo(info, true);
//			}
//		});
		return cell;
	}

}
